package com.stylefeng.guns.core.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.springframework.web.multipart.MultipartFile;

import com.stylefeng.guns.config.properties.GunsProperties;

public class UploadUtil {

    /**
     * 获取上传文件的根目录，通过application.yml配置
     * @return
     */
    public static String getRootUploadDir(){
        return SpringContextHolder.getBean(GunsProperties.class).getFileUploadPath();
    }

    /**
     * 根据目录名称获取上传文件路径，不存在则创建
     * @param dir
     * @return
     */
    public static String getFileUploadPath(String dir) {
        //如果没有写文件上传路径,保存到临时目录
        String fileUploadPath = getRootUploadDir();
        if(TextUtil.isEmpty(dir)){
            return fileUploadPath;
        }

        File file = new File(fileUploadPath, dir);
        if(!file.exists()){
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 获取文件下载路径，
     * @param dirName 所在文件夹名称。eg: email
     * @param fileName 文件名称
     * @return 文件绝对路径,不存在则为""
     */
    public static String getDownloadPath(String dirName,String fileName) {
        String dirPath = getFileUploadPath(dirName);
        File file = new File(dirPath,fileName);
        if(file.exists()){
            return file.getAbsolutePath();
        }
        return "";

    }

    /**
     * 文件上传-保存文件到指定目录
     * @param file
     * @param dirName
     * @return
     */
    public static boolean upload(MultipartFile file, String dirName){
        if(null != file){
            String fileName = file.getOriginalFilename();
            if(fileName == null) return false;
            String path = UploadUtil.getFileUploadPath(dirName);
            //获取指定文件或文件夹在工程中真实路径，getRequest()这个方法是返回一个HttpServletRequest，封装这个方法为了处理编码问题
            FileOutputStream fos = null;
            try {
                //打开FileOutStrean流
                fos = new FileOutputStream(new File(path ,fileName));
                //将MultipartFile file转成二进制流并输入到FileOutStrean
                org.apache.tomcat.util.http.fileupload.IOUtils.copy(file.getInputStream(),fos);
                fos.close();
                return true;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }else {
            return false;
        }
    }


    /**
     * 文件上传并插入数据库
     * @param file
     * @param dirName
     * @param uuid
     */
    /*public static void uploadAndInsert(MultipartFile file,String dirName,String uuid){
        if(upload(file,dirName)){
            String fileName = file.getOriginalFilename();
            String path = UploadUtil.getFileUploadPath(dirName);
            Attachments attachment = new Attachments();
            attachment.setAttachmentName(fileName);
            attachment.setAttachmentPath(path);
            attachment.setEmailCode(uuid);
            SpringContextHolder.getBean(IAttachmentsService.class).insert(attachment);
        }
    }*/


    public static ArrayList<String> getDirs(String path){
        return getFiles(path,true);
    }


    public static ArrayList<String> getFiles(String path,boolean onlyDir) {
        ArrayList<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if(onlyDir){
                if (tempList[i].isDirectory()) {
                    files.add(tempList[i].toString());
                }
            }else{
                if (tempList[i].isDirectory()) {
                    files.add(tempList[i].toString());
                }
                if (tempList[i].isFile()) {
                    files.add(tempList[i].toString());
                }
            }


        }
        return files;
    }

}
